<template>
  <view class="w-100 h92 f-b-c px-2">
    <!-- <view class="w-100 h92 f-b-c px-2" > -->
    <view class="px-2 f-y-s">
      <ImageFile
        :url="userInfo.avatar"
        @set_url_my="set_url_my"
        @click.stop="get_user"
        className="w92 h92 rounded-circle"
      />
      <!-- <div @click="$refs.signUp.open()">12323123</div>
      <div></div> -->
      <view class="pl-1">
        <view class="f-y-s h92 font700" v-if="isLogin" @click.top="set_url_my">
          <view class="f-c-c pr-2" style="height: 100%; color: #666">{{
            isRegister ? userInfo.name || "微信用户" : "未注册"
          }}</view>
          <!-- <view class="bg-theme_1 t-w h28 font20 f-c-c circle20" style="padding: 0 14rpx;">VIP{{userInfo.vip|| 0 }}</view> -->
        </view>
        <view v-else class="f-y-s h28 font700" @click="get_user">
          <view class="font32 h28 f-c-c pr-2 t-8">未登录</view>
        </view>
      </view>
    </view>

    <uni-popup ref="membership_code" type="center">
      <view class="w696 h552 circle54 bg-f8ff ov-hd show0">
        <view
          class="w-100 h146 f-c-c bg-theme radius-54-botton font40 t-0 font600"
        >
          会员码
        </view>
        <view class="w-100 h406 bg-theme re" v-if="member_code_show">
          <view class="w-100 h-100 bg-f8ff radius-54-right px-6">
            <view class="w-100 h60"></view>
            <view class="w-100 h276 f-c-c">
              <!-- {{userInfo.id}} -->
              <tki-barcode ref="barcode" :val="userInfo.id" :onval="true" />
              <!-- <u-qrcode ref="qrcode" canvas-id="qrcode" size="168" :value=""></u-qrcode> -->
            </view>
          </view>
          <view class="w-100 h100 f-c-c ab px-6 left0 bottom40">
            <view
              class="w270 h90 bg-theme font32 t-20 font600 f-c-c circle14"
              @click="set_cancel"
              >确定</view
            >
          </view>
        </view>
      </view>
    </uni-popup>

    <!-- <view
      v-if="!isRegister"
      @click.stop="$refs.signUp.open()"
      class="w166 mr-2 h66 f-c-c bg-theme circle20 t-0 font24 font600"
    >
      立即注册
    </view> -->
    <SignUp :isRegister.sync="isRegister" ref="signUp" />
  </view>
</template>

<script>
// import SignUp from "@/components/signUp.vue";
import {
  wxMiniLoginByCode,
  encryptedData,
  getMiniPhoneByCode,
  member_add,
} from "../api/auth.js";

import { mapState, mapActions } from "vuex";
import ImageFile from "./img_file.vue";

import { get_member_info, getLastYcExpiredTime } from "../api/user.js";
import tkiBarcode from "./tki-barcode/tki-barcode.vue";

export default {
  data() {
    return {
      userInfo: {},
      wx_reg: {},
      opations: { height: 80, fontSize: 24, text: "" },
      year_card_time: "",
      member_code_show: false,
      isRegister: !!uni.getStorageSync("userInfo").phone,
    };
  },
  components: {
    ImageFile,
    tkiBarcode,
    // SignUp,
  },
  computed: {
    ...mapState("user", ["isLogin"]),
  },
  watch: {
    isLogin() {
      if (!this.isLogin) {
        this.get_user();
      }
    },
  },
  mounted() {
    // this.$refs.user_register.open()
    // this.$refs.signUp.open(); //改为弹窗注册
    uni.$on("updateUserInfo", () => {
      this.userInfo = uni.getStorageSync("userInfo") || {};
    });
    this.userInfo = uni.getStorageSync("userInfo") || {};
    // token是静默登录
    const token = uni.getStorageSync("token") || "";
    if (!token) {
      this.get_wx_login();
    }
    if (this.userInfo?.userId) {
      this.get_user_info();
      this.get_year_card();
    }
  },
  beforeDestroy() {
    uni.$off("updateUserInfo");
  },
  methods: {
    set_url_year() {
      uni.redirectTo({
        url: `/pages/ticket/index`,
      });
      uni.setStorageSync("nav_code", "yeas_car");
    },
    barresult(v) {
      // console.log('code128:', v)
    },
    ...mapActions("user", ["set_change_is_login"]),
    get_user() {
      // 登录的情况下 点击后跳转到个人中心
      // 在个人中心点击跳转到 编辑
      // 未登录 去登录
      if (this.isLogin) {
      } else {
        this.get_login_show();
      }
    },
    show_purchase() {
      this.$refs.membership_code.open();
      this.member_code_show = true;
    },
    set_cancel() {
      this.member_code_show = false;
      this.$refs.membership_code.close();

      // this.$refs.user_register.close()
    },
    set_url_badge() {
      if (this.isLogin) {
        uni.navigateTo({
          url: `/pages/my/badge/badge`,
        });
      } else {
        this.get_login_show();
      }
    },
    set_url_my() {
      if (!this.isRegister) {
        this.$refs.signUp.open();
        return;
      }
      if (this.isLogin) {
        uni.navigateTo({
          url: `/pages/my/set/set`,
        });
      } else {
        this.get_login_show();
      }
    },
    get_integral() {
      if (this.isLogin) {
        uni.navigateTo({
          url: "/pages/integral/integral",
        });
      } else {
        this.get_login_show();
      }
    },
    get_login_show() {
      let is_user = uni.getStorageSync("is_user");
      if (is_user) {
        this.get_wx_login();
      } else {
        // get_auth_info

        // this.$refs.user_register.open()
        // this.get_auth_info();
        this.$refs.signUp.open(); //改为弹窗注册
      }
    },

    get_wx_login() {
      //静默登录
      if (this.isLogin) {
        return;
      }
      uni.login({
        provider: "weixin",
        success: (res) => {
          console.log(res, "res=code====");
          let data = {
            code: res.code,
          };
          wxMiniLoginByCode(data)
            .then((res) => {
              if (res.code == 0) {
                uni.setStorageSync("token", res.body.token); //存token

                // 是否 已注册
                uni.setStorageSync("is_user", !!res.body.phone);
                let userInfo = {
                  ...res.body,
                  token: res.body?.token,
                  openId: res.body?.openId
                };
                // 先存用户信息
                uni.setStorageSync("userInfo", userInfo);
                this.userInfo = userInfo;
                // 在设置是否登录 是否登录需要判断是否有用户信息
                if (this.set_change_is_login) {
                  this.set_change_is_login(true);
                }
              }
            })
            .catch((e) => {
              console.error(e);
            });
        },
        fail: () => {
          uni.showToast({
            title: "微信登录授权失败",
            icon: "none",
          });
        },
      });
    },
    get_auth_info() {
      uni.showLoading({
        title: "加载中",
      });
      //接口被回收，无法使用
      uni.getUserProfile({
        desc: "获取用户基本资料",
        lang: "zh_CN",
        success: (info) => {
          console.log("跳转-------");
          uni.setStorageSync("userInfo", info.userInfo);
          this.$refs.signUp.open(); //改为弹窗注册
          // uni.navigateTo({
          //   url: "/pages/my/register",
          // });
        },
        // 用户取消登录后的提示
        fail: (res) => {
          uni.showModal({
            title: "授权用户信息失败！",
            showCancel: false,
          });
        },
        complete: () => {
          uni.hideLoading();
        },
      });
    },
    get_user_info() {
      uni.showLoading({
        title: "加载中",
      });
      // // 没有用 先留着
      // let parmas = {
      // 	 "encryptedData": this.wx_reg.encryptedData,
      // 	 "iv": this.wx_reg.iv
      // }
      encryptedData({})
        .then((res) => {
          if (res.code == 0) {
            uni.setStorageSync("token", res.body.token); //存token
            // 是否 已注册
            uni.setStorageSync("is_user", !!res.body.memberVO);
            if (!!res.body.memberVO) {
              let userInfo = {
                ...res.body.memberVO,
                token: res.body.token,
                openId: res.body.openId,
              };
              // 先存用户信息
              uni.setStorageSync("userInfo", userInfo);
              this.userInfo = userInfo;
              // 在设置是否登录 是否登录需要判断是否有用户信息
              this.set_change_is_login && this.set_change_is_login(true);
            }
          }
        })
        .catch((e) => {
          console.error(e);
        })
        .finally(() => {
          uni.hideLoading();
        });
    },
    get_year_card() {
      // getLastYcExpiredTime().then((res) => {
      //   if (res.code == 0) {
      //     this.year_card_time = res.body?.substring(0, 10);
      //   }
      // });
    },
    // async get_member(){
    // 	const {code,body } = await get_member_info({id:this.userInfo.userId})
    // 	if(code=='0'){
    // 		let userinfo = uni.getStorageSync("userInfo")
    // 		uni.setStorageSync("userInfo",{...userinfo,...body})
    // 		this.userInfo = body
    // 	}
    // }
  },
};
</script>

<style lang="scss" scoped>
.ty-line {
  &:after {
    content: "";
    width: 1rpx;
    height: 48rpx;
    left: 124rpx;
    top: 6rpx;
    background: #ccc;
    position: absolute;
  }
}
</style>
